<template>
  <!-- 官网设置-新增编辑公共弹窗 -->
  <div class="editeCommonPopup">
    <div class="content-popup">
      <dynamicForm ref="dynamicForm" :dynamicForm="parentData"></dynamicForm>
      <div class="box-bottom">
        <el-button style="width:1.4rem;" class="cancel resetElement" type="primary" @click="cancel">取消</el-button>
        <el-button style="width:1.4rem;" :disabled="parentData.type=='query'" class="ok resetElement" type="primary" @click="save">保存</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import dynamicForm from './dynamicForm'
export default {
  components:{dynamicForm},
  props:['editeCommonPopup'],
  data() {
    return {
      /**
       * @param group:table/list 表格/列表 类型
       * @param type:add/edite/query 新增/编辑/查询 模式
       * @param visible: 组件隐藏/显示
       * @param data:数据
       */
      parentData:tool.clone(this.editeCommonPopup)
    };
  },
  methods: {
    init(){
      
    },
    // campusChange : function(campusId) { //校区改变
    //   this.init();
    // },
    cancel(){
      this.editeCommonPopup.visible=false;
    },
    save(){
      // this.getRichTxtEditorHtml();
      this.getImagesData();
      let checkobj=this.checkData();
      if(!checkobj.success){
        this.$message({
          message: checkobj.msg,
          type: checkobj.type
        });
        return;
      }
      // console.log(this.parentData.editeModelContent)
      if(this.parentData.group=='table'){
        if(this.parentData.type=='add'){
          this.$emit('callback',this.parentData.editeModelContent);
        }
        else if(this.parentData.type=='edite'){
          this.$emit('callback',this.parentData.editeModelContent);
        }
      }
      else if(this.parentData.group=='list'){
        if(this.parentData.type=='add'){
          this.$emit('callback',this.parentData.editeModelContent);
        }
        else if(this.parentData.type=='edite'){
          this.$emit('callback',this.parentData.editeModelContent);
        }
      }
      this.editeCommonPopup.editeModelContent=this.parentData.editeModelContent;
      this.editeCommonPopup.visible=false;
    },
    /**
     * 富文本编辑器获取值
     * 
     */
    // getRichTxtEditorHtml(){
    //   let fieldname=null;
    //   for(let i=0;i<this.parentData.siteModelFileds.length;i++){
    //     if(this.parentData.siteModelFileds[i].filedClass=='editor'){
    //       fieldname=this.parentData.siteModelFileds[i].filedName;
    //       break;
    //     }
    //   }
    //   if(!fieldname)return;
    //   this.parentData.editeModelContent[fieldname]=this.$refs.dynamicForm.$refs.richtxtEditor[0].getContentHtml();
    // },
    /**
     * 获取图片数据
     */
    getImagesData(){
      let data=this.parentData.siteModelFileds;
      let valueData=this.parentData.editeModelContent;
      for(let i=0;i<data.length;i++){
        let row=data[i];
        if(row.filedClass=='image'){
          this.parentData.editeModelContent[row.filedName]=
          this.$refs.dynamicForm.singleImageUploadUrl[row.filedName];
        }
        else if(row.filedClass=='images'){
          this.parentData.editeModelContent[row.filedName]=
          this.$refs.dynamicForm.multiImageUploadList[row.filedName];
        }
      }
    },
    /**数据校验
     * {"input#单文本框", "textarea#多文本框", "dateInput#日期输入" 
     *  "image#单图上传", "images#多图上传", "radio#单选", "checkbox#多选", "select#列表", 
     *  "editor#编辑器", "link#链接"}
     * @returns {success,type,msg}
     */
    checkData(){
      let data=this.parentData.siteModelFileds;
      let valueData=this.parentData.editeModelContent;
      // console.log(valueData)
      for(let i=0;i<data.length;i++){
        let row=data[i];
        if(row.isRequired=='1'){
          let value=valueData[row.filedName];
          let msg=row.filedNameLable+'不可为空！'
          //数组
          if(row.filedClass=='tag'){
            if(row.filedReferencesArray.length==0){
              return {success:false,type:'warning',msg:msg};
            }
          }
          else if(row.filedClass=='checkbox'){
            if(value.length==0){
              return {success:false,type:'warning',msg:msg};
            }
          }
          else if(row.filedClass=='images'){
            if(value.length==0){
              return {success:false,type:'warning',msg:msg};
            }
          }
          //对象
          else if(row.filedClass=='dateInput'){
            if(!value){
              return {success:false,type:'warning',msg:msg};
            }
          }
          //字符串
          else{
            if(value===''){
              return {success:false,type:'warning',msg:msg};
            }
          }

          //字段校验
          if(row.filedClass=='input'&&row.filedRegularExpression){
            msg=row.filedNameLable+'：'+(row.filedCheckTips||'格式错误');
            //正则校验
            let checkExp=new RegExp(row.filedRegularExpression);
            if(!checkExp.test(value)){
              return {success:false,type:'warning',msg:msg};
            }
          }
          else if(row.filedClass=='inputGroup'&&row.filedRegularExpression){
            msg=row.filedNameLable+'：'+(row.filedCheckTips||'格式错误');
            //正则校验
            let checkExp=new RegExp(row.filedRegularExpression);
            let nums=value.split('|');
            if(nums.length==2){
              if(!checkExp.test(nums[0])){
                return {success:false,type:'warning',msg:msg};
              }
              if(!checkExp.test(nums[1])){
                return {success:false,type:'warning',msg:msg};
              }
            }
          }

        }
      }
      return {success:true,type:'success',msg:'保存成功'};
    }
  },
  mounted() {
	  this.init();
  },
  computed: {},
  watch: {},
  created: function() {}
};
</script>

<style lang="scss">
.editeCommonPopup {
  .content-popup {
    padding: 0 0.04rem 0.2rem 0.04rem;
		font-size: 0.16rem;
    .box-bottom{
      text-align: center;
    }
  }
  // 滚动条
  .scrollbarstyle::-webkit-scrollbar {
    width: 0.05rem;
    height: 0.1rem;
  }
  .scrollbarstyle::-webkit-scrollbar-thumb {
    border-radius: 0.5rem;
    background: #BBBBBB;
  }
  .scrollbarstyle::-webkit-scrollbar-track {
    background: #F2F6FC;
  }
}
.ow_editeCommonPopup{
  top: 0.81rem !important;
  left: 2.1rem !important;
  background-color: rgba(0,0,0,0.3);
  .el-dialog{
    width: 10.4rem;
    .el-dialog__header{
      text-align: center;
    }
    .el-dialog__title{
      font-size: 0.25rem;
      font-weight: bold;
      color: #333333;
      
    }
    .el-dialog__headerbtn{
      font-size: 0.25rem !important;
    }
  }
}
</style>